package scau.erps.view.system.teacher;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import scau.erps.app.Erps;
import scau.erps.entity.SystemOrder;
import scau.erps.service.SystemOrderService;
import scau.erps.service.impl.SystemOrderServiceImpl;
import scau.erps.util.OrderGenerator;

/**
 * 生成系统订单对话框——教师
 * @author DCJ
 *
 */
public class SystemOrderGeneratorDialog extends JDialog implements ActionListener{
	private static final long serialVersionUID = 1L;
	private JTextField quantityJtf,totalJtf,maxJtf,priceJtf;
	private String year,product,market;
	private JButton generateBtn;
	public SystemOrderGeneratorDialog(String year,String product,String market){
		this.year = year;
		this.product = product;
		this.market = market;
		this.setTitle("生成系统订单");
		this.setSize(350,200);
		JPanel contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());		
		JLabel quantityLabel = new JLabel("订单数:");
		quantityJtf = new JTextField(4);
		JLabel totalLabel = new JLabel("产品数:");
		totalJtf = new JTextField(4);
		JLabel maxLabel = new JLabel("最大数:");
		maxJtf = new JTextField(4);
		JLabel priceLabel = new JLabel("参考价:");
		priceJtf = new JTextField(4);		
		JPanel panel2 = new JPanel();
		panel2.add(quantityLabel);
		panel2.add(quantityJtf);
		panel2.add(totalLabel);
		panel2.add(totalJtf);
		JPanel panel3 = new JPanel();
		panel3.add(maxLabel);
		panel3.add(maxJtf);
		panel3.add(priceLabel);
		panel3.add(priceJtf);		
		JPanel panel5 = new JPanel();
		generateBtn = new JButton("生成订单");
		generateBtn.addActionListener(this);		
		panel5.add(generateBtn);
		contentPanel.add(panel2,BorderLayout.NORTH);
		contentPanel.add(panel3,BorderLayout.CENTER);
		contentPanel.add(panel5,BorderLayout.SOUTH);
		this.setContentPane(contentPanel);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setModal(true);
		this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==generateBtn) {				
			int quantity = Integer.parseInt(quantityJtf.getText());
			int total = Integer.parseInt(totalJtf.getText());
			int max = Integer.parseInt(maxJtf.getText());
			double price = Double.parseDouble(priceJtf.getText());
			List<SystemOrder> list = OrderGenerator.generateOrder(year,product,market,quantity,total,max,price);
			SystemOrderService service = Erps.getIocInstance().get(SystemOrderServiceImpl.class);
			service.generateOrders(list);
			String message = "生成"+year+"年"+market+product+"订单"+quantity+ "张";
			JOptionPane.showMessageDialog(null, message);	
			dispose();
		}				
	}
}
